package com.dr.controller.admin;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.dr.entity.Product;
import com.dr.entity.ProductVO;
import com.dr.entity.Producttype;
import com.dr.service.ProductService;
import com.dr.util.LayUiResult;

@Controller
@RequestMapping("/admin/")
public class ProductController {
	// 注入service
	@Autowired
	ProductService productService;

	/**
	 * 全查
	 * 
	 * @param p
	 * @return 以Map的方式返回查询到的数据和行数
	 */
	@RequestMapping(value = "/findProduct.do", method = RequestMethod.GET)
	@ResponseBody // 转为json格式数据
	public Map<String, Object> findProduct(ProductVO productVO) {
		// 调用service
		List<Product> list = productService.AllProduct(productVO);
		int count = productService.getCount(productVO);
		// 返回结果都是以Map的方式返会给Layui所以返回值都是Map
		// return LayUiResult.toClient("0", "success" , count, list);
		return list != null ? LayUiResult.toClient("0", "success", count, list) : LayUiResult.toClient("1", "没有数据");
	}

	/**
	 * 添加
	 * 
	 * @param p
	 * @return返回Map
	 */

	@RequestMapping(value = "/insertProduct.do", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> insertProduct(Product p) {
		// 调用service
		int num = productService.insertProduct(p); // 返回结果 return res>0 ?
		return num > 0 ? LayUiResult.toClient("0", "添加成功") : LayUiResult.toClient("1", "添加失败");
	}

	/**
	 * 删除
	 * 
	 * @param id
	 * @return
	 */

	@RequestMapping(value = "/deleteProduct.do", method = RequestMethod.POST)

	@ResponseBody
	public Map<String, Object> deleteProduct(int id) { // 调用service
		int res = productService.deleteProduct(id); // 返回结果
		return res > 0 ? LayUiResult.toClient("0", "删除成功") : LayUiResult.toClient("1", "该图片不可删除");

	}

	/**
	 * 删除一组
	 * 
	 * @param ids
	 * @return
	 */

	@RequestMapping(value = "/deleteProducts.do", method = RequestMethod.POST)

	@ResponseBody
	public Map<String, Object> deleteProducts(String ids) {
		// 调用service
		int res = productService.deleteProducts(ids); // 返回结果
		return res > 0 ? LayUiResult.toClient("0", "删除成功") : LayUiResult.toClient("1", "该图片不可删除");
	}

	/**
	 * 修改
	 * 
	 * @param ids
	 * @return 返回
	 */
	@RequestMapping(value = "/updateProduct.do", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> updateProduct(Product p) {
		// 调用service
		int res = productService.updateProduct(p);
		// 返回结果
		return res > 0 ? LayUiResult.toClient("0", "修改成功") : LayUiResult.toClient("1", "修改失败");
	}

	/**
	 * 修改上架
	 * 
	 * @param id
	 * @param valid
	 * @return
	 */
	@RequestMapping(value = "/updateisSoldOut.do", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> updateisSoldOut(int id, String valid) {
		int isSoldOut = valid.equals("on") ? 0 : 1;
		// 调用service
		int res = productService.updateisSoldOut(id, isSoldOut);
		// 返回结果
		return res > 0 ? LayUiResult.toClient("0", "修改成功") : LayUiResult.toClient("1", "修改失败");
	}
	/**
	 * 修改是否允许被删除（0不允许，1允许，默认值是1，只要下过订单就改成0）
	 * 
	 * @param id
	 * @param valid
	 * @return
	 */
	@RequestMapping(value = "/updateisDel.do", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> updateisDel(int id, String valid) {
		int isDel = valid.equals("on") ? 1 : 0;
		// 调用service
		int res = productService.updateisDel(id, isDel);
		// 返回结果
		return res > 0 ? LayUiResult.toClient("0", "修改成功") : LayUiResult.toClient("1", "修改失败");
	}

	/**
	 * 修改是否推荐到首页（默认值是0，0不推荐，1代表推荐）
	 * 
	 * @param id
	 * @param valid
	 * @return
	 */

	@RequestMapping(value = "/updateisRecommendMain.do", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> updateisRecommendMain(int id, String valid) {
		int isRecommendMain = valid.equals("on") ? 1 : 0;
		// 调用service
		int res = productService.updateisRecommendMain(id, isRecommendMain);
		// 返回结果
		return res > 0 ? LayUiResult.toClient("0", "修改成功") : LayUiResult.toClient("1", "修改失败");
	}

	/**
	 * 
	 * @param file
	 * @param req
	 * @return
	 * @throws IllegalStateException
	 * @throws IOException
	 */
	@RequestMapping("/uploadImg.do")
	@ResponseBody
	public Map<String, Object> upload(@RequestParam("file") MultipartFile file, HttpServletRequest req)
			   throws IllegalStateException, IOException {

			  // 判断文件是否为空，空则返回失败页面
			  if (file.isEmpty()) {
			   return LayUiResult.toClient("1", "失败");
			  }
			  // 获取文件存储路径（绝对路径）
			  String path = req.getServletContext().getRealPath("/images");
			  // 获取原文件名
			  String originalName = file.getOriginalFilename();
			  //通过UUID算法生成新名字
			  String uuidName = UUID.randomUUID().toString();
			  //UUID名称加上文件的后缀名
			  String newFile = uuidName + originalName.substring(originalName.lastIndexOf("."));
			  // 创建文件实例
			  File filePath = new File(path, newFile);
			  // 如果文件目录不存在，创建目录
			  if (!filePath.getParentFile().exists()) {
			   filePath.getParentFile().mkdirs();
			   System.out.println("创建目录" + filePath);
			  }
			  // 写入文件
			  file.transferTo(filePath);
			  return LayUiResult.toClient("0", "success",0,"images/" + newFile);
			 }  
      //获取分类
	@RequestMapping(value = "/findPtype.do", method = RequestMethod.GET)
	@ResponseBody
	public List<Producttype> findPtype() {
		return productService.findPtype();
	}
	
	@RequestMapping(value = "/editImg.do", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> uploadImg(int id, String mainImgUrl,String FuImgUrl ) {
		int res = productService.editImg(id, mainImgUrl, FuImgUrl);
		// 返回结果
		
		return res > 0 ? LayUiResult.toClient("0", "修改成功") : LayUiResult.toClient("1", "修改失败");
	}
}
